import React, { Component } from 'react'
// 引入组件
import { Card, Form, Input, Checkbox, Button, message } from 'antd'
// 引入样式
import styles from './index.module.scss'
// logo图片
import Logo from 'assets/logo.png'

// 请求
import { login } from 'api/user'

// 存token
import { setToken } from 'utils/storage'

export default class Login extends Component {
  state = {
    loding: false
  }
  render() {
    return (
      <div className={styles.login}>
        {/* 登录框 */}
        <Card className={styles['login-container']}>
          {/* logo */}
          <img src={Logo} alt="" className={styles['login-logo']} />
          {/* 表单
          onFinish的value拿到的是一个对象,每个属性对应Form.Item每个name对应表单的值
          */}
          <Form
            size="large"
            onFinish={this.onFinish}
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true
            }}
          >
            <Form.Item
              name="mobile"
              validateTrigger={'onBlur'}
              rules={[
                { required: true, message: '请输入手机号' },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '手机号格式错误'
                }
              ]}
            >
              <Input placeholder="请输入手机号" autoComplete="off" />
            </Form.Item>

            <Form.Item
              name="code"
              validateTrigger={'onBlur'}
              rules={[
                { required: true, message: '请输入验证码' },
                {
                  pattern: /^\d{6}$/,
                  message: '验证码格式错误'
                }
              ]}
            >
              <Input placeholder="请输入验证码" autoComplete="off" />
            </Form.Item>

            <Form.Item
              valuePropName="checked"
              name="agree"
              rules={[
                { required: true, message: '请阅读并同意用户协议和隐私条款' },
                () => ({
                  validator(_, value) {
                    if (value) {
                      return Promise.resolve()
                    } else {
                      return Promise.reject('请阅读并同意用户协议和隐私条款')
                    }
                  }
                })
              ]}
            >
              <Checkbox>
                我已阅读并同意 <span style={{ color: 'blue' }}>用户协议</span>和
                <span style={{ color: 'blue' }}>隐私条款</span>
              </Checkbox>
            </Form.Item>

            <Form.Item>
              <Button
                type="primary"
                htmlType="submit"
                block
                loading={this.state.loading}
              >
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }
  // 登录
  onFinish = async ({ mobile, code }) => {
    // loading状态
    this.setState({
      loading: true
    })
    try {
      // 发请求
      const res = await login(mobile, code)
      // 弹出提示
      message.success('登录成功', 1, () => {
        // 存储token
        setToken(res.data.token)
        this.props.history.push('/home')
        // const { state } = this.props.location
        // console.log(this.props.location)
        // if (state) {
        //   this.props.history.push(state.from)
        // } else {
        //   // 跳转到首页
        //   this.props.history.push('/home')
        // }
      })
    } catch (err) {
      // 弹出提示
      message.warning(err.response.data.message, 1)
    }
    // 关闭loading
    this.setState({
      loading: false
    })
  }
}
